package com.kl.model.pojo;

import com.kl.App;
import com.kl.TankGame;
import com.kl.constant.TankModel;
import com.kl.constant.Windows;
import com.kl.enums.Direct;
import com.kl.enums.TankType;
import com.kl.exceptions.DeathException;
import com.kl.model.base.AbstractAttack;
import com.kl.model.base.ShootFactory;

import java.lang.ref.WeakReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author CC
 * time 2024-05-15
 * description
 */
public class EnemyTank extends Tank   {
    public EnemyTank( int x, int y) {
        super(TankType.ENEMY, x, y);
        this.setSpeed(2);
        this.setTimes(3);
    }

    @Override
    public boolean attack(int x, int y, Direct direct, Object object, TankType origin) {
        //新增子弹库存
        CopyOnWriteArrayList<AbstractAttack> attacks = getAttacks();
        if (attacks==null) {
           throw new DeathException();
        }
        if (attacks.size()>3) {
            return false;
        }
        for (int i = 0; i < getTimes(); i++) {
            Shoot bean = ShootFactory.getObject(x, y, direct,origin);
            CompletableFuture.runAsync(bean, App.getPOOL());
            attacks.add(bean);
        }
        try {
            int interval1 = getInterval();
            if (interval1==0) {
                return false;
            }
            Thread.sleep(interval1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    /**
     * 是否撞墙
     */
    private boolean isHit = false;

    @Override
    public void move(int x, int y, int speed, Direct direct) {
        this.setDirect(direct);
        super.isTouch();
        switch (direct){
            case UP:
                if(y- TankModel.PIPED_LENGTH -speed>0){
                    this.setY(y-speed);
                }else {
                    isHit=true;
                }
                break;
            case DOWN:
                if(y+TankModel.PIPED_LENGTH+speed< Windows.HEIGHT){
                    this.setY(y + speed);
                }else {
                    isHit=true;
                }
                break;
            case LEFT:
                if(x-TankModel.PIPED_LENGTH-speed>0){
                        this.setX(x - speed);
                }else {
                    isHit=true;
                }
                break;
            case RIGHT:
                if(x+TankModel.PIPED_LENGTH+speed<Windows.WIDTH){
                    this.setX(x+speed);
                }else {
                    isHit=true;
                }
                break;
        }

    }

    @Override
    public void run() {
        while (!isDestroyed() || !TankGame.isEnd()){
            Direct random = Direct.random();
            try{
                this.attack(getX(),getY(),random,null,getOrigin());
            }catch (DeathException e){
                break;
            }
            isHit=false;
            for (int i = 0; i < 20; i++) {
                move(getX(), getY(), getSpeed(), random);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    return;
                }
                if(isHit){
                    break;
                }
            }
        }
    }
}
